﻿-- Author      : Kurapica
-- Create Date : 8/03/2008 17:14

--------------------------
-- SingleTextBox        --
--------------------------
do
	-- Check Version
	local version = 1

	if not IGAS:RegisterFile("IGAS.GUI.Widget.SingleTextBox", version) then
		return
	end
		
    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
	local _WidgetName = "SingleTextBox"
    local _Base = "EditBox"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _Enum = IGAS.GUI.Base.Enum
	local _PropertyType = IGAS.GUI.Base.PropertyType
	
    -- Style
    local TEMPLATE_COMMON = "COMMON"
    local TEMPLATE_LIGHT = "LIGHT"
	
    -- Define Block
    _Enum["TextBoxStyle"] = {
        TEMPLATE_COMMON,
		TEMPLATE_LIGHT,
    }
	
    _PropertyType["TextBoxStyle"] = {
        ["Type"] = "enum",
		["EnumType"] = _Enum.TextBoxStyle,
    }
	
    -- Scripts
    local _FrameBackdropLight = {
        bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        tile = true, tileSize = 16, edgeSize = 16,
        insets = { left = 3, right = 3, top = 3, bottom = 3 }
    }
    
    local function OnEscapePressed(self, ...)		
	    self:ClearFocus()
    end
    
	local function OnEditFocusLost(self, ...)		
		self:HighlightText(0, 0)
    end
	
    local function OnEditFocusGained(self, ...)        
	    self:HighlightText()
    end
    
	-- ScriptType
	local _ScriptType = {
    }
		
	--	FuncProxy
	local _FuncProxy = {
		-- Used for ButtonTemplate
        ["SetStyle"] = function(self, style)
            local chk = false
            local t
            
            -- Check Style
            if not style or type(style) ~= "string" then
                return
            end
            
            for _, v in ipairs(_Enum["TextBoxStyle"]) do
                if v == style then
                    chk = true
                    break
                end
            end
            
            if (not chk) or style == self.__Style then
                return
            end
            
            -- Change Style
            if style == TEMPLATE_COMMON then
				if self:GetChild("BackGround") then
					self:GetChild("BackGround"):Dispose()
				end
				
				local left = CreateFrame("Texture", "LEFT", self, "BACKGROUND")
				left.Width = 8
				left:SetTexture("Interface\\Common\\Common-Input-Border")
				left:SetTexCoord(0, 0.0625, 0, 0.625)
				left:SetPoint("TOPLEFT", self, "TOPLEFT", -5, 0)
				left:SetPoint("BOTTOMLEFT", self, "BOTTOMLEFT", -5, 0)
								
				local right = CreateFrame("Texture", "RIGHT", self, "BACKGROUND")
				right.Width = 8
				right:SetTexture("Interface\\Common\\Common-Input-Border")
				right:SetTexCoord(0.9375, 1.0, 0, 0.625)
				right:SetPoint("TOPRIGHT", self, "TOPRIGHT", 0, 0)
				right:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 0, 0)

				local middle = CreateFrame("Texture", "MIDDLE", self, "BACKGROUND")
				middle.Width = 10
				middle:SetTexture("Interface\\Common\\Common-Input-Border")
				middle:SetTexCoord(0.0625, 0.9375, 0, 0.625)
				middle:SetPoint("TOPLEFT", left, "TOPRIGHT", 0, 0)
				middle:SetPoint("TOPRIGHT", right, "TOPLEFT", 0, 0)
				middle:SetPoint("BOTTOMLEFT", left, "BOTTOMRIGHT", 0, 0)
				middle:SetPoint("BOTTOMRIGHT", right, "BOTTOMLEFT", 0, 0)
            elseif style == TEMPLATE_LIGHT then
				if self:GetChild("LEFT") then
					self:GetChild("LEFT"):Dispose()
				end
				if self:GetChild("RIGHT") then
					self:GetChild("RIGHT"):Dispose()
				end
				if self:GetChild("MIDDLE") then
					self:GetChild("MIDDLE"):Dispose()
				end
				
                local back = CreateFrame("Frame", "BackGround", self)
		        back.FrameLevel = self.FrameLevel - 1
		        back:SetPoint("TOPLEFT", self, "TOPLEFT", -5, 3)
				back:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 5, -3)        
				back:SetBackdrop(_FrameBackdropLight)
				back:SetBackdropColor(0, 0, 0)
		        back:SetBackdropColor(0, 0, 0, 1)
            end
        end,
        
        ["GetStyle"] = function(self)
            return self.__Style or TEMPLATE_NONE
        end,
	}
	
	--	Property
	local _Property = {
		-- Style
        ["Style"] = {
			["Set"] = function(self, style)
                self:SetStyle(style)
			end,
			
			["Get"] = function(self)
				return self:GetStyle()
			end,
			
			["Type"] = _PropertyType.TextBoxStyle,
		},
	}
	
    local function _New(parent)
		-- New Frame
		local txtBox = CreateFrame(_Base,nil,parent)
        txtBox.Height = 20
        txtBox.FontObject = "GameFontNormal"
        txtBox.MouseEnabled = true
        txtBox.AutoFocus = false
        
        local back = CreateFrame("Frame", "BackGround", txtBox)
        back.FrameLevel = txtBox.FrameLevel - 1
        back:SetPoint("TOPLEFT", txtBox, "TOPLEFT", -5, 3)
		back:SetPoint("BOTTOMRIGHT", txtBox, "BOTTOMRIGHT", 5, -3)        
		back:SetBackdrop(_FrameBackdropLight)
		back:SetBackdropColor(0, 0, 0)
        back:SetBackdropColor(0, 0, 0, 1)
        
        txtBox:StackScript("OnEscapePressed", OnEscapePressed)
        txtBox:StackScript("OnEditFocusLost", OnEditFocusLost)
        txtBox:StackScript("OnEditFocusGained", OnEditFocusGained)
        
		return txtBox
	end	
	
	-- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
end
